package com.datatools.ui.frame;

import com.datatools.entity.LocalFunction;
import com.datatools.entity.LocalProcedure;
import com.datatools.entity.LocalTable;
import com.datatools.entity.LocalView;
import com.datatools.i18n.BaseTextResource;

import javax.swing.*;
import javax.swing.tree.TreePath;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;

public class LeftPanelTreeMouseAdapter extends MouseAdapter {

    private BaseTextResource baseTextResource = BaseTextResource.getInstance();
    //事件源对象
    private JTree source;

    @Override
    public void mouseClicked(MouseEvent e) {

        try {
            if(source == e.getSource()) {
                // 按照鼠标点击的坐标点获取路径
                TreePath selPath = source.getPathForLocation(e.getX(), e.getY());
                source.setSelectionPath(selPath);
                //鼠标右键
                if(e.getButton() == MouseEvent.BUTTON3) {
                    if (selPath != null) {
                        LeftPanelTreeNode node = (LeftPanelTreeNode) selPath.getLastPathComponent();
                        System.out.println("右键："+selPath + " ," + node.toString());
                    } else {
                        System.out.println("右键：空白处");
                    }
                }else if(e.getButton() == MouseEvent.BUTTON1){
                    //鼠标左键
                    if(e.getClickCount()==2){
                        //双击事件
                        if (selPath != null) {
                            LeftPanelTreeNode node = (LeftPanelTreeNode) selPath.getLastPathComponent();
                            Object treeNodeUserObject = node.getUserObject();
                            switch (selPath.getPathCount()-1){
                                case 0:
                                    //root
                                    System.out.println("双击根节点");
                                    break;
                                case 1:
                                    //连接节点
                                    System.out.println("双击连接节点");
                                    break;
                                case 2:
                                    //库节点，重新连接(刷新)
                                    System.out.println("双击数据库节点");
                                    break;
                                case 3:
                                    // 视图、表、函数、过程 父节点
                                    if(baseTextResource.getTable().equals(treeNodeUserObject)){
                                        LeftPanel.getInstance().getTables(node,"TABLE");
                                    }else if(baseTextResource.getView().equals(treeNodeUserObject)){
                                        LeftPanel.getInstance().getTables(node,"VIEW");
                                    }else if(baseTextResource.getFunction().equals(treeNodeUserObject)){
                                        LeftPanel.getInstance().getFunctions(node);
                                    }else if(baseTextResource.getProcedure().equals(treeNodeUserObject)){
                                        LeftPanel.getInstance().getProcedures(node);
                                    }
                                    break;
                                case 4:
                                    // 单个视图、表、函数、过程节点
                                    if(treeNodeUserObject instanceof LocalTable){
                                        LeftPanel.getInstance().getTableColums(node);
                                        LeftPanel.getInstance().getTableIndexs(node);

                                    }else if(treeNodeUserObject instanceof LocalView){
                                        LeftPanel.getInstance().getTableColums(node);
                                    }else if(treeNodeUserObject instanceof LocalFunction){
                                    }else if(treeNodeUserObject instanceof LocalProcedure){
                                    }
                                    break;
                                case 5:
                                    // 视图列、表的列、索引、触发器 父节点

                                    break;
                                case 6:
                                    // 单个列、索引、触发器
                                    break;

                            }
                            source.repaint();
                            System.out.println("左键双击：level="+node.getLevel()+selPath+" ," +node.toString());
                        }
                    }
                }
            }
        } catch (SQLException ex) {
            ex.printStackTrace();
        }
    }

    public JTree getSource() {
        return source;
    }

    public void setSource(JTree source) {
        this.source = source;
    }
}
